package com.sywb.chuangyebao.contract;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.Column;
import com.sywb.chuangyebao.bean.Feedback;
import com.sywb.chuangyebao.bean.InterestIndustryInfo;
import com.sywb.chuangyebao.bean.Live;
import com.sywb.chuangyebao.bean.Page;
import com.sywb.chuangyebao.bean.QARichInfo;
import com.sywb.chuangyebao.bean.Question;
import com.sywb.chuangyebao.bean.User;
import com.sywb.chuangyebao.bean.UserFavorite;
import com.sywb.chuangyebao.bean.VideoInfo;
import com.sywb.chuangyebao.utils.DateTimeUtils;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.RegexUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.NewsDetailActivity;
import com.sywb.chuangyebao.view.QAndADetailActivity;
import com.sywb.chuangyebao.view.TopicActivity;
import com.sywb.chuangyebao.view.TrainMediaDetailActivity;
import com.sywb.chuangyebao.view.VideoDetailActivity;
import com.sywb.chuangyebao.widget.RichTextView;

import org.bining.footstone.BaseConstants;
import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemChildClickListener;
import org.bining.footstone.adapter.listener.OnItemClickListener;
import org.bining.footstone.adapter.listener.OnLoadMoreListener;
import org.bining.footstone.db.DbManager;
import org.bining.footstone.db.assit.QueryBuilder;
import org.bining.footstone.log.Logger;
import org.bining.footstone.utils.TimeUtils;
import org.bining.footstone.view.BaseActivity;
import org.bining.footstone.widget.PullToRefreshView;

import java.util.LinkedHashMap;
import java.util.List;

public interface ListCheckContract {

    interface ListCheckView extends BaseRefreshContract.View {

        void setAskConfirmText(LinkedHashMap<Integer, String> checked);
    }

    class Presenter extends BaseRefreshContract.Presenter<ListCheckView> {

        private GoodAtAdapter goodAtAdapter;
        private CollectionAdapter collectionAdapter;
        private QuestionAdapter questionAdapter;
        private FeedbackAdapter feedbackAdapter;
        private AskAdapter askAdapter;
        private LinkedHashMap<Integer, String> askList;
        private InviteAdapter inviteAdapter;
        private VideoAdapter videoAdapter;
        private LiveAdapter liveAdapter;
        private AudioAdapter audioAdapter;
        private SpecialAdapter specialAdapter;

        private int pageNo = 1;

        @Override
        public void onStart() {
            super.onStart();
        }

        @Override
        public void initData() {
        }

        public void initGoodAtView() {
            pullToRefreshView.setRefreshEnable(false);

            recyclerDivider.setDivider(0);

            List<InterestIndustryInfo> list = DbManager.getInstance().query(new QueryBuilder<InterestIndustryInfo>(InterestIndustryInfo.class).appendOrderAscBy("sort"));

            goodAtAdapter = new GoodAtAdapter(mActivity);
            goodAtAdapter.setDatas(list);
            goodAtAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(android.view.View view, int i) {
                    InterestIndustryInfo goodAreaInfo = goodAtAdapter.getItem(i);
                    if (goodAreaInfo.isChecked) {
                        goodAreaInfo.isChecked = false;
                    } else {
                        int count = 0;
                        for (int j = 0; j < goodAtAdapter.getItemCount(); j++) {
                            if (goodAtAdapter.getItem(j).isChecked) {
                                count++;
                            }
                        }
                        goodAreaInfo.isChecked = true;
//                        if (count < 5) {
//                            goodAreaInfo.isChecked = true;
//                        } else {
//                            showMessage("最多选择5个擅长领域");
//                            return;
//                        }
                    }
                    goodAtAdapter.notifyDataSetChanged();
                }
            });

            commonRecycler.setAdapter(goodAtAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
        }

        public void saveGoodArea() {
            StringBuilder goodAreaIds = new StringBuilder();
            for (InterestIndustryInfo info : goodAtAdapter.getDatas()) {
                if (info.isChecked) {
                    goodAreaIds.append(info.categoryId).append(Constants.COMMA);
                }
            }
            if (goodAreaIds.length() > 0) {
                goodAreaIds.delete(goodAreaIds.length() - 1, goodAreaIds.length());
                HttpUtils.goodatUpdate(0, goodAreaIds.toString(), 0, new HttpCallback<Object>(goodAreaIds.toString()) {
                    @Override
                    public void onSuccess(Object o) {
                        List<InterestIndustryInfo> goodAreaInfos = goodAtAdapter.getDatas();
                        DbManager.getInstance().save(goodAreaInfos);

                        // String openID = SharedUtils.getString(Constants.USEROPENID, "");
                        // UserData userData = DbManager.getInstance().queryById(openID, UserData.class);
                        // userData.expert_in_field = "[" + data + "]";
                        // DbManager.getInstance().save(userData);

                        if (mView != null) {
                            mView.exit();
                        }
                    }

                    @Override
                    public void onError(String strMsg) {
                        super.onError(strMsg);
                        showMessage(strMsg);
                    }
                });
            } else {
                showMessage("请至少选择1个擅长领域");
            }
        }

        public void initCollectionView() {
            collectionAdapter = new CollectionAdapter(mActivity);
            commonRecycler.setAdapter(collectionAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));

            collectionAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                    // 类型 question 为问题 article为文章 media为多媒体 column为专栏
                    UserFavorite userFavorite = collectionAdapter.getItem(i);
                    switch (userFavorite.topic_type) {
                        case Constants.TOPIC_TYPE_QUESTION:
                            mView.advance(QAndADetailActivity.class, userFavorite.topic_id);
                            break;
                        case Constants.TOPIC_TYPE_ARTICLE:
                            mView.advance(NewsDetailActivity.class, userFavorite.topic_id);
                            break;
                        case Constants.TOPIC_TYPE_VIDEO:
                            switch (userFavorite.video_type) {
                                case 1:
                                    mView.advance(VideoDetailActivity.class, userFavorite.topic_id);
                                    break;
                                case 2:
                                    mView.advance(TrainMediaDetailActivity.class, "视频详情", String.valueOf(userFavorite.topic_id));
                                    break;
                            }
                            break;
                        case Constants.TOPIC_TYPE_AUDIO:
                            mView.advance(TrainMediaDetailActivity.class, "音频详情", String.valueOf(userFavorite.topic_id));
                            break;
                        case Constants.TOPIC_TYPE_SPECIAL:
                            mView.advance(TrainMediaDetailActivity.class, "专题详情", String.valueOf(userFavorite.topic_id));
                            break;
                    }
                }
            });

            pullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshView pullToRefreshView) {
                    pageNo = 1;
                    getFavoriteList();
                }
            });

            collectionAdapter.openLoadMore(R.layout.layout_loading, Constants.PAGESIZE);
            collectionAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMoreClick() {
                    pageNo++;
                    getFavoriteList();
                }
            });

            onStartAsync();
            getFavoriteList();
        }

        public void getFavoriteList() {
            HttpUtils.favoriteList(pageNo, new HttpCallback<List<UserFavorite>>() {
                @Override
                public void onSuccess(List<UserFavorite> itemStyles) {
                    refreshList(itemStyles, collectionAdapter);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    refreshList(null, collectionAdapter);
                    showMessage(strMsg);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });
        }

        public void initQuestionView() {
            questionAdapter = new QuestionAdapter(mActivity);
            commonRecycler.setAdapter(questionAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));

            questionAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                    goQuestionDetail(questionAdapter.getItem(i).question_id);
                }
            });

            pullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshView pullToRefreshView) {
                    pageNo = 1;
                    initDataQuestions();
                }
            });

            questionAdapter.openLoadMore(R.layout.layout_loading, Constants.PAGESIZE);
            questionAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMoreClick() {
                    pageNo++;
                    initDataQuestions();
                }
            });

            pageNo = 1;
            onStartAsync();
            initDataQuestions();
        }

        private void goQuestionDetail(int id) {
            mView.advance(QAndADetailActivity.class, id);
        }


        private void initDataQuestions() {
            HttpUtils.wendaQuestion(pageNo, new HttpCallback<List<Question>>() {
                @Override
                public void onSuccess(List<Question> userQuestions) {
                    refreshList(userQuestions, questionAdapter);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    refreshList(null, questionAdapter);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });
        }


        public void initFeedBackView() {
            feedbackAdapter = new FeedbackAdapter(mActivity);
            commonRecycler.setAdapter(feedbackAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));

            pullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshView pullToRefreshView) {
                    pageNo = 1;
                    initFeedBackData();
                }
            });

            feedbackAdapter.openLoadMore(R.layout.layout_loading, Constants.PAGESIZE);
            feedbackAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMoreClick() {
                    pageNo++;
                    initFeedBackData();
                }
            });

            onStartAsync();
            initFeedBackData();
        }

        private void initFeedBackData() {
            HttpUtils.listFeedbacks(pageNo, new HttpCallback<List<Feedback>>() {
                @Override
                public void onSuccess(List<Feedback> feedbacks) {
                    refreshList(feedbacks, feedbackAdapter);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    refreshList(null, feedbackAdapter);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });
        }


        public void initAskView(String users) {
            askList = new LinkedHashMap<>();

            if (!TextUtils.isEmpty(users)) {
                List<QARichInfo> richInfoList = JSON.parseArray(users, QARichInfo.class);
                if (richInfoList != null && richInfoList.size() > 0) {
                    for (QARichInfo richInfo : richInfoList) {
                        askList.put(richInfo.uid, richInfo.richName);
                    }
                    mView.setAskConfirmText(askList);
                }
            }

            Logger.e("@转换后：" + JSON.toJSONString(askList));
            askAdapter = new AskAdapter(mActivity);
            commonRecycler.setAdapter(askAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));

            askAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(android.view.View view, int i) {
                    User user = askAdapter.getItem(i);
                    if (askList.containsKey(user.user_id)) {
                        askList.remove(user.user_id);
                    } else if (askList.size() == 5) {
                        showMessage("sorry,最多只能选5个");
                        return;
                    } else {
                        askList.put(user.user_id, "@" + user.display_name + BaseConstants.SPACE);
                    }
                    askAdapter.notifyDataSetChanged();
                    mView.setAskConfirmText(askList);
                }
            });

            pullToRefreshView.setRefreshEnable(false);
            pullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshView pullToRefreshView) {
                    pageNo = 1;
                    initDataTutor();
                }
            });

            askAdapter.openLoadMore(R.layout.layout_loading, Constants.PAGESIZE);
            askAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMoreClick() {
                    pageNo++;
                    initDataTutor();
                }
            });

            onStartAsync();
            initDataTutor();
        }

        private void initDataTutor() {
            HttpUtils.listUserTutor(0, Constants.PAGESIZE, pageNo, false, -1, 0, "", "", new HttpCallback<Page<User>>() {
                @Override
                public void onSuccess(Page<User> userPage) {
                    refreshPage(userPage, askAdapter);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                    refreshPage(null, askAdapter);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });
        }


        private <T> void refreshList(List<T> list, BaseRecyclerAdapter<T> adapter) {
            if (mView == null) {
                return;
            }

//            mView.hideProgress();
            if (pageNo == 1) {
                onEndRefresh();
                adapter.clearDatas();
            } else {
                onEndLoadMore();
            }

            if (isNotNull(list)) {
                adapter.notifyDataChangedAfterLoadMore(list);
            }

            mView.setNoData(adapter.getDataCount() == 0);
        }


        private <T> void refreshPage(Page<T> pageInfo, BaseRecyclerAdapter<T> adapter) {
            if (mView == null) {
                return;
            }

//            mView.hideProgress();
            if (pageNo == 1) {
                onEndRefresh();
                adapter.clearDatas();
            } else {
                onEndLoadMore();
            }

            if (pageInfo != null) {
                adapter.setLoadMoreEnable(pageInfo.page < pageInfo.lastPageNo);
                if (pageInfo.list != null) {
                    adapter.notifyDataChangedAfterLoadMore(pageInfo.list);
                }
            }
            mView.setNoData(adapter.getDataCount() == 0);
        }

        public void initSearchPeopleView() {
            inviteAdapter = new InviteAdapter(mActivity);
            commonRecycler.setAdapter(inviteAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));

            pullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshView pullToRefreshView) {
                    pageNo = 1;
                    getPeople();
                }
            });

            inviteAdapter.openLoadMore(R.layout.layout_loading, Constants.PAGESIZE);
            inviteAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMoreClick() {
                    pageNo++;
                    getPeople();
                }
            });
            inviteAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
//                    mView.advance(UserIndexActivity.class, inviteAdapter.getItem(i).user_id);
                    Utils.goUserIndex(mView, inviteAdapter.getItem(i).user_id);
                }
            });
        }

        private int questionId;
        private String keyword;

        public void listPeople(int questionId, String keyword) {
            this.questionId = questionId;
            this.keyword = keyword;
            getPeople();
        }

        private void getPeople() {
            HttpUtils.listUserTutor(0, Constants.PAGESIZE, pageNo, false, -1, questionId, keyword, "", new HttpCallback<Page<User>>() {
                @Override
                public void onSuccess(Page<User> userPage) {
                    refreshPage(userPage, inviteAdapter);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                    refreshPage(null, inviteAdapter);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });
        }


        public void initVideoView() {
            videoAdapter = new VideoAdapter(mActivity);
            commonRecycler.setAdapter(videoAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
            videoAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(android.view.View view, int i) {
                    mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.video_detail), String.valueOf(videoAdapter.getItem(i).media_id));
                }
            });

            pullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshView pullToRefreshView) {
                    pageNo = 1;
                    initTrainVideoData();
                }
            });

            videoAdapter.openLoadMore(R.layout.layout_loading, Constants.PAGESIZE);
            videoAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMoreClick() {
                    pageNo++;
                    initTrainVideoData();
                }
            });

            initTrainVideoData();
        }

        private void initTrainVideoData() {
            initTrainData(1, videoAdapter);
        }


        private void initTrainData(int video_type, final BaseRecyclerAdapter adapter) {
            HttpUtils.getVideoList(video_type, -1, pageNo, 2, new HttpCallback<Page<VideoInfo>>() {
                @Override
                public void onSuccess(Page<VideoInfo> videoPage) {
                    refreshList(videoPage.list, adapter);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                    refreshList(null, adapter);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });

        }


        public void initLiveView() {
            liveAdapter = new LiveAdapter(mActivity);
            commonRecycler.setAdapter(liveAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));

            liveAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(android.view.View view, int i) {
                    mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.live), String.valueOf(liveAdapter.getItem(i).live_id));
                }
            });

            pullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshView pullToRefreshView) {
                    pageNo = 1;
                    listLive();
                }
            });

            liveAdapter.openLoadMore(R.layout.layout_loading, Constants.PAGESIZE);
            liveAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMoreClick() {
                    pageNo++;
                    listLive();
                }
            });

            onStartAsync();
            listLive();
        }

        private void listLive() {
            HttpUtils.listLive(pageNo, new HttpCallback<Page<Live>>() {
                @Override
                public void onSuccess(Page<Live> livePage) {
                    refreshPage(livePage, liveAdapter);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                    refreshList(null, liveAdapter);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }

            });
        }

        public void initAudioView() {
            audioAdapter = new AudioAdapter(mActivity);
//            audioAdapter.setDatas(Arrays.asList(mActivity.getResources().getStringArray(R.array.trade_types)));
            commonRecycler.setAdapter(audioAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
            audioAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(android.view.View view, int i) {
                    mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.audio_detail), String.valueOf(audioAdapter.getItem(i).media_id));
                }
            });

            pullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshView pullToRefreshView) {
                    pageNo = 1;
                    initTrainAudioData();
                }
            });

            audioAdapter.openLoadMore(R.layout.layout_loading, Constants.PAGESIZE);
            audioAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMoreClick() {
                    pageNo++;
                    initTrainAudioData();
                }
            });

            onStartAsync();
            initTrainAudioData();
        }

        private void initTrainAudioData() {
            initTrainData(2, audioAdapter);
        }

        public void initSpecialView() {
            specialAdapter = new SpecialAdapter(mActivity);
//            adapter.setDatas(Arrays.asList(mActivity.getResources().getStringArray(R.array.trade_types)));
            commonRecycler.setAdapter(specialAdapter);
            commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
            recyclerDivider.setDivider(mActivity.getResources().getDimensionPixelSize(R.dimen.dp_15));
            recyclerDivider.setColor(ContextCompat.getColor(mActivity, R.color.backgroundGray));
            specialAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(android.view.View view, int i) {
                    mView.advance(TrainMediaDetailActivity.class, mActivity.getString(R.string.special_detail), String.valueOf(specialAdapter.getItem(i).column_id));
                }
            });

            pullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshView pullToRefreshView) {
                    pageNo = 1;
                    initSpecialData();
                }
            });

            specialAdapter.openLoadMore(R.layout.layout_loading, Constants.PAGESIZE);
            specialAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMoreClick() {
                    pageNo++;
                    initSpecialData();
                }
            });

            onStartAsync();
            initSpecialData();
        }

        private void initSpecialData() {
            HttpUtils.listColumn(pageNo, new HttpCallback<Page<Column>>() {
                @Override
                public void onSuccess(Page<Column> columnPage) {
                    refreshPage(columnPage, specialAdapter);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                    refreshPage(null, specialAdapter);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });
        }

        class GoodAtAdapter extends BaseRecyclerAdapter<InterestIndustryInfo> {

            int[] trade_types;

            GoodAtAdapter(Activity activity) {
                super(activity, R.layout.item_good_at);
                trade_types = new int[]{
                        R.drawable.icon_communication,
                        R.drawable.icon_delicious,
                        R.drawable.icon_clothes,
                        R.drawable.icon_life,
                        R.drawable.icon_drink,
                        R.drawable.icon_home,
                        R.drawable.icon_beauty,
                        R.drawable.icon_car,
                        R.drawable.icon_decorate,
                        R.drawable.icon_present,
                        R.drawable.icon_machinery,
                        R.drawable.icon_baby,
                        R.drawable.icon_education
                };
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, InterestIndustryInfo goodAreaInfo) {
                viewHolderHelper.setText(R.id.tv_type_name, goodAreaInfo.name);
                viewHolderHelper.setImageResource(R.id.iv_type, trade_types[position]);
                viewHolderHelper.setVisibility(R.id.iv_check, goodAreaInfo.isChecked ? android.view.View.VISIBLE : android.view.View.GONE);
            }
        }

        class CollectionAdapter extends BaseRecyclerAdapter<UserFavorite> {

            CollectionAdapter(Activity activity) {
                super(activity, R.layout.item_collection);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, UserFavorite model) {

                //设置提问人头像
                ImageView imageFace = viewHolderHelper.getView(R.id.iv_face);
                GlideUtils.getCircleImageToUrl(mActivity, model.avatar_s, imageFace, R.drawable.head_image);

                //设置提问人角色类型
                Utils.setUserRoleIcon((ImageView) viewHolderHelper.getView(R.id.iv_user_type), model.user_role);
                //设置提问人名称
                viewHolderHelper.setText(R.id.tv_collection_name, model.nickname);

                //设置提问时间
                viewHolderHelper.setText(R.id.tv_collection_time, DateTimeUtils.getFormatForMinusMonthDayMinutesSecond(model.date_time));

//                //隐藏头部右边 的 推荐 按钮
                viewHolderHelper.setVisibility(R.id.tv_recommend, View.GONE);

                viewHolderHelper.setText(R.id.tv_media_name, model.title);

                ImageView ivImage = viewHolderHelper.getView(R.id.iv_media);
                ImageView ivMediaType = viewHolderHelper.getView(R.id.iv_media_type);

                boolean ivVisible;
                boolean mediaTypeVisible;
                switch (model.topic_type) {
                    case Constants.TOPIC_TYPE_AUDIO:
                        //语音
                        GlideUtils.getNormalIamgeToUrl(mActivity, model.pic, ivImage);
                        ivMediaType.setImageResource(R.drawable.icon_media_audio);
                        viewHolderHelper.setVisibility(R.id.rl_layout, View.VISIBLE);
                        ivVisible = true;
                        mediaTypeVisible = true;
                        break;
                    case Constants.TOPIC_TYPE_VIDEO:
                        //视频
                        GlideUtils.getNormalIamgeToUrl(mActivity, model.pic, ivImage);
                        ivMediaType.setImageResource(R.drawable.icon_media_video);
                        viewHolderHelper.setVisibility(R.id.rl_layout, View.VISIBLE);
                        ivVisible = true;
                        mediaTypeVisible = true;
                        break;
                    default:
                        if (TextUtils.isEmpty(model.pic)) {
                            //文字
                            viewHolderHelper.setVisibility(R.id.rl_layout, View.GONE);
                            ivVisible = false;
                            mediaTypeVisible = false;
                        } else {
                            //图片
                            GlideUtils.getNormalIamgeToUrl(mActivity, model.pic, ivImage);
                            viewHolderHelper.setVisibility(R.id.rl_layout, View.VISIBLE);
                            ivVisible = true;
                            mediaTypeVisible = false;
                        }
                        break;
                }

                ivImage.setVisibility(ivVisible ? View.VISIBLE : View.GONE);
                ivMediaType.setVisibility(mediaTypeVisible ? View.VISIBLE : View.GONE);
            }
        }

        class QuestionAdapter extends BaseRecyclerAdapter<Question> implements OnItemChildClickListener {


            QuestionAdapter(Activity activity) {
                super(activity, R.layout.item_question);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, Question model) {
//                viewHolderHelper.setText(R.id.richTextView, model.title);
                viewHolderHelper.setText(R.id.tv_answer_num, RegexUtils.getFormatNum(model.answer_num) + "回答  ·   " + RegexUtils.getFormatNum(model.collectCount) + "收藏")
                        .setText(R.id.tv_question_time, DateTimeUtils.getFormatForMinusMonthDayMinutesSecond(model.create_time));

                //设置富文本
                RichTextView richTextView = viewHolderHelper.getView(R.id.richTextView);
                viewHolderHelper.setItemChildClickListener(R.id.richTextView);
                viewHolderHelper.setOnItemChildClickListener(this);
                richTextView.setRichText(model.title, model.richInfoList);
                richTextView.setOnLinkClickListener(new RichTextView.OnLinkClickListener() {
                    @Override
                    public void onAtClick(RichTextView richTextView, Object userId) {
//                        mView.advance(UserIndexActivity.class, userId);
                        Utils.goUserIndex(mView, (Integer) userId);
                    }

                    @Override
                    public void onTopicClick(RichTextView richTextView, Object topicId) {
                        //若当前已为话题聚合页，则不跳转
                        mView.advance(TopicActivity.class, topicId);
                    }

                    @Override
                    public void onEmojiClick(RichTextView richTextView, Object emoji) {

                    }

                    @Override
                    public void onUrlClick(RichTextView richTextView, Object url) {

                    }

                    @Override
                    public void onAllClick(RichTextView richTextView) {

                    }
                });
            }

            @Override
            public void onItemChildClick(View view, int i) {
                switch (view.getId()) {
                    case R.id.richTextView:
                        goQuestionDetail(mDatas.get(i).question_id);
                        break;
                }
            }
        }

        class FeedbackAdapter extends BaseRecyclerAdapter<Feedback> {


            FeedbackAdapter(Activity activity) {
                super(activity, R.layout.item_feedback);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, Feedback model) {
                GlideUtils.getCircleImageToUrl(mActivity, model.avatar_hd, (ImageView) viewHolderHelper.getView(R.id.iv_face));
                viewHolderHelper.setText(R.id.tv_feedback_name, model.nickname).setText(R.id.tv_feedback_time, TimeUtils.mdhm(model.creat_time * 1000L)).setText(R.id.tv_feedback, model.cont);
                TextView textView = viewHolderHelper.getView(R.id.tv_answer);
                if (TextUtils.isEmpty(model.remark)) {
                    textView.setVisibility(View.GONE);
                } else {
                    textView.setVisibility(View.VISIBLE);
                    textView.setText(Html.fromHtml("<font color=\'#D43D3D\'>系统回复：</font>" + model.remark));
                }
            }
        }

        class AskAdapter extends BaseRecyclerAdapter<User> {


            AskAdapter(Activity activity) {
                super(activity, R.layout.item_questions_to_people);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, User model) {
                ImageView imageView = viewHolderHelper.getView(R.id.iv_tutor_face);
                GlideUtils.getCircleImageToUrl(mActivity, model.avatar, imageView);

                Utils.setUserRoleIcon((ImageView) viewHolderHelper.getView(R.id.iv_user_type), model.user_role);

                viewHolderHelper.setImageResource(R.id.iv_tutor_chose, askList.containsKey(model.user_id) ? R.drawable.chose_fill : R.drawable.chose_normal)
                        .setText(R.id.tv_tutor_name, model.display_name)
                        .setText(R.id.tv_tutor_other, model.signature);
            }
        }

        public class InviteAdapter extends BaseRecyclerAdapter<User> implements OnItemChildClickListener {

            InviteAdapter(Activity activity) {
                super(activity, R.layout.item_friend);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, User model) {
                viewHolderHelper.setVisibility(R.id.tv_add_focus, View.GONE)
                        .setVisibility(R.id.tv_invite, View.VISIBLE)
                        .setText(R.id.tv_friend_name, model.display_name)
                        .setText(R.id.tv_friend_sign, model.getGoodAtString())
                        .setOnItemChildClickListener(this);
                viewHolderHelper.setItemChildClickListener(R.id.tv_invite);

                ImageView imageView = viewHolderHelper.getView(R.id.iv_friend_face);
                GlideUtils.getCircleImageToUrl(mActivity, model.avatar, imageView);

                //设置提问人角色类型
                Utils.setUserRoleIcon((ImageView) viewHolderHelper.getView(R.id.iv_user_type), model.user_role);
                Utils.setUserRoleText((TextView) viewHolderHelper.getView(R.id.tv_friend_type), model.user_role);

                TextView tvInvite = viewHolderHelper.getView(R.id.tv_invite);
                if (model.isSelected) {
                    tvInvite.setText(R.string.invited);
                    tvInvite.setTextColor(ContextCompat.getColor(mContext, R.color.colorGray));
                    tvInvite.setBackgroundResource(R.drawable.round_line_gray);
                } else {
                    tvInvite.setText(R.string.invite);
                    tvInvite.setTextColor(ContextCompat.getColor(mContext, R.color.colorBlueLight));
                    tvInvite.setBackgroundResource(R.drawable.round_line_blue_light);
                }
            }

            @Override
            public void onItemChildClick(View view, int i) {
                if (!((BaseActivity) mActivity).isCanClick(view)) return;
                final User user = mDatas.get(i);
                switch (view.getId()) {
                    case R.id.tv_invite:
                        if (user.isSelected) {
                            return;
                        }

                        HttpUtils.invite(user.user_id, questionId, new HttpCallback<Object>() {

                            @Override
                            public void onStart() {
                                super.onStart();
                                onStartAsync();
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();
                                onFinishAsync();
                            }

                            @Override
                            public void onSuccess(Object o) {
                                if (mView == null) {
                                    return;
                                }

                                user.isSelected = true;
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onError(String strMsg) {
                                super.onError(strMsg);
                                showMessage(strMsg);
                            }
                        });

                        break;
                }
            }
        }

        class VideoAdapter extends BaseRecyclerAdapter<VideoInfo> {


            VideoAdapter(Activity activity) {
                super(activity, R.layout.item_train_special);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, VideoInfo model) {
                GlideUtils.getNormalIamgeToUrl(mActivity, model.media_logo, (ImageView) viewHolderHelper.getView(R.id.iv_special));

                viewHolderHelper
                        .setText(R.id.tv_special_name, model.media_title)
                        .setText(R.id.tv_special_text, model.media_info)
                        .setText(R.id.tv_special_time, DateTimeUtils.getMediaTime(model.media_length) + "   " + model.click_num + "人观看");
            }
        }


        class LiveAdapter extends BaseRecyclerAdapter<Live> {


            LiveAdapter(Activity activity) {
                super(activity, R.layout.item_train_live);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, Live model) {
                GlideUtils.getNormalIamgeToUrl(mActivity, model.thumb_pic, (ImageView) viewHolderHelper.getView(R.id.iv_live));

                viewHolderHelper
                        .setText(R.id.tv_live_name, model.title)
                        .setText(R.id.tv_live_person, model.live_person)
                        .setText(R.id.tv_special_text, model.live_level)
                        .setText(R.id.tv_live_date, TimeUtils.mdhm(model.live_time));

                //状态：1=直播中，2待播，3，已播
                TextView tvTime = viewHolderHelper.getView(R.id.tv_live_time);
                tvTime.setVisibility(model.status == 2 ? View.VISIBLE : View.GONE);
                ImageView imageView = viewHolderHelper.getView(R.id.iv_live_state);
                TextView tvState = viewHolderHelper.getView(R.id.tv_live_state);
                switch (model.status) {
                    case 1:
                        imageView.setImageResource(R.drawable.live_broadcast);
                        tvState.setText("直播中");
                        tvState.setTextColor(ContextCompat.getColor(mActivity, R.color.colorBlue));
                        break;
                    case 2:
                        imageView.setImageResource(R.drawable.alarm_clock);
                        tvState.setText("直播预约");
                        tvState.setTextColor(ContextCompat.getColor(mActivity, R.color.colorTheme));
                        tvTime.setText(DateTimeUtils.getDays(model.live_time));
                        break;
                    case 3:
                        imageView.setImageResource(R.drawable.playback);
                        tvState.setText("直播回看");
                        tvState.setTextColor(ContextCompat.getColor(mActivity, R.color.colorGrayText));
                        break;
                }

            }
        }


        class AudioAdapter extends BaseRecyclerAdapter<VideoInfo> {


            AudioAdapter(Activity activity) {
                super(activity, R.layout.item_train_audio_list);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, VideoInfo model) {
                GlideUtils.getCircleImageToUrl(mActivity, model.media_logo, (ImageView) viewHolderHelper.getView(R.id.iv_audio));

                viewHolderHelper
                        .setText(R.id.tv_audio_name, model.media_title)
                        .setText(R.id.tv_audio_where, model.media_info)
                        .setText(R.id.tv_audio_time, DateTimeUtils.getMediaTime(model.media_length));
            }
        }

        class SpecialAdapter extends BaseRecyclerAdapter<Column> {

            SpecialAdapter(Activity activity) {
                super(activity, R.layout.item_train_special_list);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int position, Column model) {

                GlideUtils.getNormalIamgeToUrl(mActivity, model.thumb_pic, (ImageView) viewHolderHelper.getView(R.id.iv_special));

                viewHolderHelper
                        .setText(R.id.tv_special_name, model.title)
                        .setText(R.id.tv_special_text, model.des);
            }
        }
    }

}

